﻿namespace Creek.UI.EFML.Base.Controls.Navigator
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Windows.Forms;

    using global::EFML.Properties;

    public class PageNavigator : UserControl, IUIElement
    {
        #region Fields

        public List<UserControl> Pages = new List<UserControl>();

        private HeadlessTabControl headlessTabControl1;

        private bool initialized;

        private Label label1;

        private TravelButton travelButton1;

        #endregion

        #region Constructors and Destructors

        public PageNavigator()
        {
            this.InitializeComponent();
            this.BackGround = Resources.BACKGROUND_arrowless;
            this.NavVisible = true;
            this.label1.Text = "";
        }

        #endregion

        #region Public Properties

        public Image BackGround
        {
            get
            {
                return this.travelButton1.BackGround;
            }
            set
            {
                this.travelButton1.BackGround = value;
            }
        }

        public Image BackwardButton
        {
            get
            {
                return this.travelButton1.BackwardButton;
            }
            set
            {
                this.travelButton1.BackwardButton = value;
            }
        }

        public Color ChildColor
        {
            get
            {
                return this.Pages[this.CurrentPage].BackColor;
            }
            set
            {
                this.Pages[this.CurrentPage].BackColor = value;
            }
        }

        public string Content { get; set; }

        public int CurrentPage
        {
            get
            {
                return this.headlessTabControl1.SelectedIndex;
            }
            set
            {
                this.headlessTabControl1.SelectedIndex = value;
            }
        }

        public Image ForwardButton
        {
            get
            {
                return this.travelButton1.ForwardButton;
            }
            set
            {
                this.travelButton1.ForwardButton = value;
            }
        }

        public string ID { get; set; }

        public bool NavVisible { get; set; }

        public Color TitleColor
        {
            get
            {
                return this.BackColor;
            }
            set
            {
                this.BackColor = value;
            }
        }

        public IValidator Validator { get; set; }

        public IStyle style { get; private set; }

        #endregion

        #region Public Methods and Operators

        public void Back()
        {
            this.NavigateTo(this.CurrentPage - 1);
            if (this.CurrentPage == 0)
            {
                this.travelButton1.BackEnabled = false;
            }
            this.travelButton1.ForwardEnabled = true;
        }

        public void Forward()
        {
            this.NavigateTo(this.CurrentPage + 1);
            this.travelButton1.BackEnabled = true;
            if (this.CurrentPage == this.headlessTabControl1.TabPages.Count - 1)
            {
                this.travelButton1.ForwardEnabled = false;
            }
        }

        public void NavigateTo(int i)
        {
            this.CurrentPage = i;
            this.label1.Text = this.headlessTabControl1.TabPages[i].Text;
        }

        public void RefreshPages()
        {
            foreach (var page in this.Pages)
            {
                var tp = new TabPage();
                tp.Controls.Add(page);
                tp.Name = page.Text;
                tp.Text = page.Text;
                this.headlessTabControl1.TabPages.Add(tp);
            }
            this.NavigateTo(0);
        }

        #endregion

        #region Methods

        protected override void OnEnter(EventArgs e)
        {
            if (!this.initialized)
            {
                //RefreshPages();
                this.initialized = true;
            }
            base.OnEnter(e);
        }

        private void InitializeComponent()
        {
            this.headlessTabControl1 = new HeadlessTabControl();
            this.travelButton1 = new TravelButton();
            this.label1 = new Label();
            this.SuspendLayout();
            // 
            // headlessTabControl1
            // 
            this.headlessTabControl1.Dock = DockStyle.Fill;
            this.headlessTabControl1.Location = new Point(0, 29);
            this.headlessTabControl1.Name = "headlessTabControl1";
            this.headlessTabControl1.SelectedIndex = 0;
            this.headlessTabControl1.Size = new Size(406, 246);
            this.headlessTabControl1.TabIndex = 1;
            // 
            // travelButton1
            // 
            this.travelButton1.BackEnabled = false;
            this.travelButton1.BackToolTip = null;
            this.travelButton1.Dock = DockStyle.Top;
            this.travelButton1.ForwardToolTip = null;
            this.travelButton1.Location = new Point(0, 0);
            this.travelButton1.Name = "travelButton1";
            this.travelButton1.Size = new Size(406, 29);
            this.travelButton1.TabIndex = 0;
            this.travelButton1.TabStop = false;
            this.travelButton1.Text = "travelButton1";
            this.travelButton1.ItemClicked += this.travelButton1_ItemClicked;
            // 
            // label1
            // 
            this.label1.Anchor = (((((AnchorStyles.Top | AnchorStyles.Bottom) | AnchorStyles.Left) | AnchorStyles.Right)));
            this.label1.AutoSize = true;
            this.label1.Location = new Point(173, 13);
            this.label1.Name = "label1";
            this.label1.Size = new Size(27, 13);
            this.label1.TabIndex = 2;
            this.label1.Text = "Title";
            // 
            // PageNavigator
            // 
            this.Controls.Add(this.label1);
            this.Controls.Add(this.headlessTabControl1);
            this.Controls.Add(this.travelButton1);
            this.Name = "PageNavigator";
            this.Size = new Size(406, 275);
            this.ResumeLayout(false);
            this.PerformLayout();
        }

        private void travelButton1_ItemClicked(object sender, TravelButtonItemClickedEventArgs e)
        {
            if (e.ClickedItem == TravelButtonItem.BackButton)
            {
                this.Back();
            }
            if (e.ClickedItem == TravelButtonItem.ForwardButton)
            {
                this.Forward();
            }
        }

        #endregion
    }
}